package com.sinog.front.controller.biz;

import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import com.sinog.front.config.log.Log;
import com.sinog.front.feign.biz.EncryptionService;
import com.sinog.front.feign.biz.MaintenanceService;
import com.sinog.front.feign.biz.PageSelectService;
import com.sinog.front.feign.biz.TimingeExecuteSqlService;
import com.sinog.front.service.user.UserFacade;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @desc 定时执行sql访问层——暂时把其他的运维也放到这，之后可以把运维的抽到一起
 * @author ybl
 * @date 2022/10/10
 */
@Slf4j
@RestController
public class TimingeExecuteSqlController {

    @Resource
    private UserFacade userFacade;
    @Resource
    private TimingeExecuteSqlService timingeExecuteSqlService;
    @Resource
    private EncryptionService encryptionService;
    @Resource
    private PageSelectService pageSelectService;
    @Resource
    private MaintenanceService maintenanceService;

    /**
     * 查询所有的定时执行的SQL
     * @return String
     */
    @GetMapping("/azbj/dsqx/selectAll")
    public String selectTimingSql(@RequestParam(required = false) Map<String,Object> params){
        UserDTO authentication = userFacade.getAuthentication();
        if(Constants.RoleId.ROLE_ID_AB.equals(authentication.getRoleid())) {
            return timingeExecuteSqlService.selectTimingSql(params);
        }
        return ReturnCode.NO_PERMISSION.message();
    }

    /**
     * 定时清洗SQL-【删除】sql
     * @param request 日志留痕获取地址用到
     * @param id 要删除的id
     * @return Result
     */
    @Log(type = "删除", value = "定时清洗SQL-【删除】sql")
    @DeleteMapping("/azbj/dsqx/delSql/{id}")
    public Result delSql(HttpServletRequest request,@PathVariable String id){
        UserDTO authentication = userFacade.getAuthentication();
        if(Constants.RoleId.ROLE_ID_AB.equals(authentication.getRoleid())) {
            return timingeExecuteSqlService.delSql(id);
        }
        return Result.failure(ReturnCode.NO_PERMISSION);
    }

    /**
     * 添加/更新SQL
     * @param request 日志留痕获取地址用到
     * @param data 前台数据
     * @return Result
     */
    @Log(type = "新增/更新", value = "定时清洗SQL-【添加/跟新】sql")
    @PostMapping("/azbj/dsqx/addSql")
    public Result addSql(HttpServletRequest request,String data){
        String ip = "";
        ip = request.getHeader("X-Forwarded-For");
        if(null == ip || ip.isEmpty()) {
            ip = request.getRemoteAddr();
        } else {
            // 如果有多级反向代理，返回的是一组ip，取第一个
            ip = ip.split(",")[0];
        }
        Map<String,Object> map = JsonUtil.toMap(data);
        map.put("ip",ip);
        return timingeExecuteSqlService.addSql(map);
    }

    /*==========================账号加密==============================*/
    /**
     * 运维功能-账号加密
     * @param data 前台传值
     * @return Result
     */
    @PostMapping("/azbj/maintenance/dataEncryption")
    public Result getEncryptionData(String data){
        UserDTO authentication = userFacade.getAuthentication();
        if(Constants.RoleId.ROLE_ID_AB.equals(authentication.getRoleid())) {
            return encryptionService.encryptionData(data);
        }
        return Result.failure(ReturnCode.NO_PERMISSION);
    }

    /*==========================账号加密==============================*/

    /**
     * 运维功能-页面select查询
     * @param data 前台传值
     * @return Result
     */
    @PostMapping("/azbj/maintenance/getPageSelect")
    public Result getPageSelect(String data){
        UserDTO authentication = userFacade.getAuthentication();
        if(Constants.RoleId.ROLE_ID_AB.equals(authentication.getRoleid())) {
            return pageSelectService.getPageSelect(data);
        }
        return Result.failure(ReturnCode.NO_PERMISSION);
    }

    /**
     * 运维功能-删除罪犯人员信息
     * @param codeId 目的省区域编码
     * @param priNum 罪犯人员编号
     * @return Result
     */
    @PostMapping("/azbj/ywgl/ryql")
    public Result deleteRyByPriNum(String codeId,String priNum){
        return pageSelectService.deleteRyByPriNum(codeId,priNum);
    }

    /*==========================一键生成账号==============================*/

    /**
     * 查询一键生成账号
     * @return String
     */
    @GetMapping("/azbj/maintenance/selectAccountAll")
    public String selectAccountAll(HttpServletRequest request){
        UserDTO authentication = userFacade.getAuthentication();
        if(Constants.RoleId.ROLE_ID_AB.equals(authentication.getRoleid())) {
            return maintenanceService.selectAccountAll();
        }
        return ReturnCode.NO_PERMISSION.message();
    }

    /**
     * 一键生成账号
     * @return Result
     */
    @Log(type = "新增", value = "一键生成账号 -【新增】账号")
    @GetMapping("/azbj/maintenance/generateAccount")
    public Result generateAccount(HttpServletRequest request){
        UserDTO authentication = userFacade.getAuthentication();
        if(Constants.RoleId.ROLE_ID_AB.equals(authentication.getRoleid())) {
            return maintenanceService.generateAccount();
        }
        return Result.failure(ReturnCode.NO_PERMISSION);
    }

    /**
     * 删除一键生成的账号和数据
     * @param serialNumber 当前序列号
     * @return Result
     */
    @Log(type = "删除", value = "一键生成账号 -【删除】账号")
    @DeleteMapping("/azbj/maintenance/delAutoAccount/{serialNumber}")
    public Result delAutoAccount(HttpServletRequest request,@PathVariable String serialNumber){
        UserDTO authentication = userFacade.getAuthentication();
        if(Constants.RoleId.ROLE_ID_AB.equals(authentication.getRoleid())) {
            return maintenanceService.delAutoAccount(serialNumber);
        }
        return Result.failure(ReturnCode.NO_PERMISSION);
    }
}